# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='slim'
pkgname="ruby-${_gemname}"
pkgver=4.1.0
pkgrel=3
pkgdesc='A template language whose goal is reduce the syntax to the essential parts without becoming cryptic'
arch=('any')
url="https://github.com/slim-template/${_gemname}"
license=('MIT')
depends=('ruby' 'ruby-temple' 'ruby-tilt')
checkdepends=('ruby-kramdown' 'ruby-pandoc-ruby' 'ruby-rack-test' 'ruby-sassc' 'ruby-sinatra')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}_fix_gemspec.patch"
        "${pkgname}_fix_openstruct.patch::https://github.com/slim-template/slim/commit/15286536094c84c5cb28cd9a8997346e0f314178.patch"
        "${pkgname}_fix_tests.patch")
sha512sums=('0507781497ba5bca0bd6a9bca50d5454b9c0325219eafd760eec57aa2f19d85e42a03d296f52375ef84db8307cb369a461bc1d6d2f4caccaa83e5a351386f333'
            '8e7f1d8859c6275eafcb061a98e767722b78bcd553a453605a8b2812d66885079d8e92c8823a11b490e38ae39cd594afed7ef7dc16ecb23588ead56ed3ab85c8'
            '0d18fb9e7ef9677b0755ee407e71199c6902695b22203ca86a88468da095575b3e97572666fd5494d821e5fd886f102c66245f3400fca733e256f1680c18d201'
            '74c43dfef0de1f3398c29ca6166923762a40c5eb031894cdc7b6b2dff956b2d17eb0f56ad6304a3b6674e1d18144f6f460e8ecfdb321ae78bfb62a4749dbdd5c')

prepare() {
  cd "${_gemname}-${pkgver}"

  patch --verbose --strip=1 --input="../${pkgname}_fix_gemspec.patch"
  patch --verbose --strip=1 --input="../${pkgname}_fix_openstruct.patch"
  patch --verbose --strip=1 --input="../${pkgname}_fix_tests.patch"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  # replace git based file detection as we don't build from a git source
  sed --in-place --regexp-extended 's|git ls-files -- bin/\*|find bin -type f|g' "${_gemname}.gemspec"
  sed --in-place --regexp-extended 's|git ls-files|find . -type f -not -path "\*/\.git/*"|g' "${_gemname}.gemspec"

  # remove rails tests
  sed --in-place '/def test_rails/,+10d' test/core/test_commands.rb
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install/${_gemdir}/cache/" \
    "tmp_install/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "tmp_install/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "tmp_install/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "tmp_install/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete
}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE* --target-directory "${pkgdir}/usr/share/licenses/${pkgname}"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
